package shop.xmall.system.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import shop.xmall.framework.core.controller.BaseController;
import shop.xmall.framework.core.domain.ResponseResultDto;
import shop.xmall.framework.core.page.PageDto;
import shop.xmall.framework.core.tree.NgTree;
import shop.xmall.system.domain.SysDept;
import shop.xmall.system.service.ISysDeptService;

import java.util.List;

/**
 * 部门Controller
 * 
 * @author xmall
 * @date 2019-10-14
 */
@Controller
@RequestMapping("/system/dept")
public class SysDeptController extends BaseController {

    @Autowired
    private ISysDeptService sysDeptService;

    /**
    * 查询部门信息列表
    */
    @PostMapping("/list")
    @ResponseBody
    public ResponseResultDto list(SysDept sysDept) {
        PageHelper.offsetPage(getPageNum() - 1, getPageSize());
        List<SysDept> list = sysDeptService.selectSysDeptList(sysDept);
        PageInfo pageInfo = new PageInfo(list);
        PageDto pageDto = new PageDto(list, pageInfo.getTotal(), getPageNum(), getPageSize());
        return ResponseResultDto.success(pageDto);
    }



    /**
     * 查询部门信息列表
     */
    @PostMapping("/treeList")
    @ResponseBody
    public ResponseResultDto treeList(SysDept sysDept) {
        List<SysDept> list = sysDeptService.selectSysDeptList(sysDept);
        List<NgTree<SysDept>> ngTrees = sysDeptService.convertTreeToList(list);
        return ResponseResultDto.success(ngTrees);
    }


    /**
     * 查询部门信息
     */
    @PostMapping("/getById")
    @ResponseBody
    public ResponseResultDto getById(Long deptId) {
        return ResponseResultDto.success(sysDeptService.selectSysDeptById(deptId));
    }


    /**
     * 新增保存部门信息
     */
    @PostMapping("/add")
    @ResponseBody
    public ResponseResultDto addSave(SysDept sysDept) {
        return toAjax(sysDeptService.insertSysDept(sysDept));
    }


    /**
     * 修改保存部门信息
     */
    @PostMapping("/edit")
    @ResponseBody
    public ResponseResultDto editSave(SysDept sysDept) {
        return toAjax(sysDeptService.updateSysDept(sysDept));
    }

    /**
     * 删除部门信息
     */
    @PostMapping("/delete")
    @ResponseBody
    public ResponseResultDto remove(String ids) {
        return toAjax(sysDeptService.deleteSysDeptByIds(ids));
    }
}
